<template>
	<view>
		<view class="">
			<image style="width: 100%;" src="/static/img/httpindex/login/login_bg.png" mode="widthFix"></image>
		</view>
		<view class="login_main">
			<view class="login_logo">
				<image style="width: 100%;height: 100%;border-radius: 50%;" src="@/static/logo.png" mode=""></image>
			</view>
			<view class="input_phone">
				<image style="width: 26rpx;margin-right: 66rpx;" src="/static/img/httpindex/login/shoujihao.png"
					mode="widthFix"></image>
				<input style="font-size: 28rpx;" type="number" placeholder="请输入手机号" placeholder-style="font-size:28rpx;color:#CCCCCC"
					v-model="username">
			</view>
			<view class="input_phone" style="margin-top: 10rpx;">
				<image style="width: 26rpx;margin-right: 66rpx;" src="/static/img/httpindex/login/mima54.png"
					mode="widthFix"></image>
				<input style="font-size: 28rpx;" type="password" minlength="6" placeholder="请输入登陆密码"
					placeholder-style="font-size:28rpx;color:#CCCCCC " v-model="password">
			</view>
			<view style="margin-top: 22rpx;">
				<label style="display: flex;align-items: center;">
					<checkbox-group @change="selectItem">
						<checkbox style="transform:scale(0.55)" :checked="checked"  />
					</checkbox-group>
					<view style="font-size: 22rpx;">
						我已阅读并同意<text style="color: #2faaff;" @tap="goService">《用户服务协议》</text>
					</view>
				</label>
			</view>
			<view class="btn_login" @click="submit">
				登录
			</view>
			<view class="bottom_cls">
				<view class="btn_zoom" @tap="resetPwd">
					忘记密码?
				</view>
				<view class="btn_zoom" @tap="register">
					<text style="color: #1B1B1B;">没有账号？</text>去注册
				</view>
			</view>
		</view>
		<slideCode :modelValue="modelValue"></slideCode>
	</view>
</template>

<script>
	import slideCode from '@/components/slide_code/xlg-slideCode.vue'
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		components: {
			slideCode
		},
		data() {
			return {
				modelValue: {
					show: false,
				},

				backgroundColor: '#29ad97',
				checked: false,
				showPassword: true,
				eve: "/static/img/login/biyan.png",
				eve_avtion: "/static/img/login/zhengyan.png",
				username: '',
				password: '',
				type: 1
			}
		},

		computed: {
			...mapState(['hasLogin', 'userInfo']),

		},

		onLoad(options) {
			options.type = this.type

			uni.$on('login_code_success', () => {
				console.log('login')
				this.confirmLogin();
			})
		},
		methods: {
			...mapMutations(['login', 'logout']),

			Focus() {
				this.backgroundColor = "#29ad97"
			},
			Blur() {
				this.backgroundColor = "#29ad97"
			},
			changePassword() {
				if (this.password.length > 0) {
					this.showPassword = !this.showPassword
				}
			},

			selectItem() {
				this.checked = !this.checked;
				// console.log(this.checked);
			},

			register() {
				uni.navigateTo({
					url: './register'
				})
			},
			captcha() {
				uni.navigateTo({
					url: './captcha'
				})
			},

			resetPwd() {
				uni.navigateTo({
					url: 'password'
				})
			},

			// 表单验证
			submit() {
				// console.log(this.checked);
				if (!this.checked) {
					this.$api.msg('请勾选用户服务协议')
					return
				}
				
				uni.setStorageSync('code_type', 'login')
				this.modelValue.show = true;
			},

			async confirmLogin() {
				let reg = await this.$myRequest({
					url: '/login',
					methods: 'POST',
					data: {
						username: this.username,
						type: this.type,
						password: this.password,
					},
				})

				this.$api.msg('登录成功！');
				setTimeout(() => {
					let token = reg.data.data.token;

					uni.setStorageSync('token', token)
					this.goLogin(token)
				}, 1500)
			},

			async goLogin(token) {
				let UIfo = await this.$myRequest({
					url: '/getUserInfo',
					methods: 'POST',
					data: {
						token: token
					}
				})

				let userInfo = UIfo.data.data

				this.login(userInfo)
				// uni.showToast({
				// 	title: '登陆成功',
				// 	icon: "none"
				// })
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			index() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			},

			goService() {
				uni.navigateTo({
					url: '/pages/docs/service-contract',
				})
			},
		},
	}
</script>

<style lang="scss">
	page {
		background: #F9F9F9;
	}

	.login_main {
		height: 740rpx;
		background: #fff;
		border-radius: 10rpx;
		position: relative;
		margin: 0 30rpx;
		top: -160rpx;
		padding: 0 47rpx;
	}

	.login_logo {
		border: 1px solid #1CBCB8;
		border-radius: 50%;
		color: #14AFAB;
		font-size: 40rpx;
		width: 120rpx;
		height: 120rpx;
		text-align: center;
		line-height: 120rpx;
		position: relative;
		top: -50rpx;
		left: 50%;
		transform: translateX(-50%);
		background: #fff;
	}

	.input_phone {
		display: flex;
		align-items: center;
		padding: 22rpx 30rpx;
		box-sizing: border-box;
		border-bottom: 1px solid #f4f4f4;
		font-size: 30rpx;
	}

	.btn_login {
		margin-top: 72rpx;
		background: #1B1B1B;
		border-radius: 70px;
		color: #fff;
		font-size: 26rpx;
		text-align: center;
		padding: 20rpx 0;
	}

	.bottom_cls {
		font-size: 24rpx;
		color: #14B6B2;
		display: flex;
		margin-top: 40rpx;
		justify-content: space-between;
	}
</style>